package web.gkw.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Component;

import web.gkw.dao.GeneralDAO;
import web.gkw.model.General;

@Component("generalDAO")
public class GeneralDAOImpl implements GeneralDAO {
	
	private final static String hql_queryAll = 
			"from general";
	private final static String hql_queryCount = 
			"select count(*) from general";
	
	@Resource
	private SessionFactory sessionFactory;

	@Override
	public boolean addGeneral(General general) {
		// TODO Auto-generated method stub
		String res = sessionFactory.getCurrentSession().save(general).toString();
		int r = Integer.parseInt(res);
		if(r > 0) {
			return true;
		}
		return false;
	}

	@Override
	public List<General> getGeneralFirst(int page) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery(hql_queryAll);
		int maxRow = 20 * page;
		int minRow = maxRow - 20;
		query.setFirstResult(minRow);
		query.setMaxResults(maxRow);
		return query.list();
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery(hql_queryCount);
		int res = ((Long)query.uniqueResult()).intValue();
		return res;
	}

	@Override
	public General getGeneral(int id) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
		General res = (General)session.get(General.class, id);
		return res;
	}

}
